<?php 
session_start();
if (empty($_SESSION['login'])){
	header("location:login.php");
	//$msg = "empty";
}
include 'functions.php';




?>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Accueil - Projet XML</title>
		<link href="libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
		<link href="css/style.css" rel="stylesheet" media="screen">
	</head>
	<body>
            
                <?php 
                if(isset($_POST['create'])) // Creation d'une facture
                {      
                    $errors = txtToXml( $_POST['bill'], $_POST['filename']);
                    if(0 == count($errors))
                        echo "Facture crée";
                    else
                    {
                        foreach($errors as $error)
                        {
                            echo '<p class="text-error">' . $error . '</p>';
                        }
                    }
                }
                
                if(isset($_GET['delete']))
                {
                    if("admin" == $_SESSION['permission'])
                        deleteBill($_GET['delete']);
                    else
                        echo '<p class="text-error">Vous n\'avez pas l\'autorisation nécessaire pour supprimer cette facture.</p>';
                        
                }
                    
                ?>
            
		<div id="document">
			<h1>Projet XML</h1>
			<div id="logout"><p><?php echo $_SESSION['login'];?>.<a href="logout.php">Deconnexion</a></p></div>
			
			
			<table class="table table-striped table-bordered" id="list_bills">
				<thead>
					<tr>
						<th>Factures</th>
						<th style="width: 100px;">Actions</th>
					</tr>
				</thead>
				<?php
				// On liste les fichiers xml
				
				$dir =opendir('bills');
				
				while ($file = readdir($dir)) 
				{	
					if($file != '.' && $file != '..' && $file != 'index.php') 
					{
						echo "<tr>";
							echo "<td><a href='bills/$file' >$file</a></td><td><a href='index.php?delete=$file' class='btn' target='blank'> Supprimer</a></td>";
						echo "</tr>";
					}
				}
				closedir($dir);
				?>
			</table>
			
                        <h4> Exemple de facture </h4>
                        <div class="well">
                           start<br>
                            dest+Bar:Joe:75 rue Landre:93100:Montreuil:France:08.36.65.65.65<br>
                            exp+Dupont:Thierry:16 rue Lol:93100:Montreuil:Angleterre:08.36.65.65.65<br>
                            dat+2012-12-12:2012-12-12<br>
                            art+666:Dentifrice:12<br>
                            cond+Neuf<br>
                            pai+Cheque:150<br>
                           end<br>
                        </div>
			
			
			<form action="" method="post" class="form-horizontal">
                               
				<div class="control-group">
                                        <h4 class="controls">Entrez une nouvelle facture :</h4>
                                        <div class="controls">Nom de la facture : <input type="text" name="filename"/></div>
					<div class="controls">
						<textarea name="bill" style="width:650px; height:250px;" ><?php if(!empty($_POST['create'])) echo $_POST['bill']; ?></textarea>
					</div>
				</div>
				<div class="control-group">
					<div class="controls">
						<button name="create" id="create" type="submit" class="btn">Valider</button>
					</div>
				</div>
			</form>
			
		</div>
	</body>
</html>

